using System;
using System.Data;
using System.Text;
using System.Xml;
using vs.Core;

namespace Forum
{
    public partial class Sitemap : System.Web.UI.Page
    {
        private const int MAXURL = 50000;
        private void GenSitemapTopic(XmlDocument doc, string topicdesk)
        {
            string strSQL = "select TopicID from Forum_Topic order by ModifyDate DESC";
            DataTable dt = DataFactory.LoadPage(strSQL, 0, MAXURL);
            XmlElement root = doc.DocumentElement;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string link = "http://" + PortalUtils.SiteName + UrlProvider.DesktopUrl(topicdesk, dt.Rows[i]["TopicID"].ToString());
                XmlElement url = doc.CreateElement("url");
                CommonFunction.AddNode(doc, url, "loc", link);
                CommonFunction.AddNode(doc, url, "lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                CommonFunction.AddNode(doc, url, "changefreq", "daily");
                CommonFunction.AddNode(doc, url, "priority", "0.8");
                root.AppendChild(url);
            }
        }

        private void GenSitemapForum(XmlDocument doc, string forumdesk)
        {
            string strSQL = "select * from Forum_Forum";
            DataTable dt = DataFactory.LoadPage(strSQL, 0, MAXURL);
            XmlElement root = doc.DocumentElement;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string link = "http://" + PortalUtils.SiteName + UrlProvider.DesktopUrl(forumdesk, dt.Rows[i]["ForumID"].ToString());
                XmlElement url = doc.CreateElement("url");
                CommonFunction.AddNode(doc, url, "loc", link);
                CommonFunction.AddNode(doc, url, "lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                CommonFunction.AddNode(doc, url, "changefreq", "daily");
                CommonFunction.AddNode(doc, url, "priority", "0.8");
                root.AppendChild(url);
            }
        }

        private string GenSitemap()
        {
            ModSettingController db = new ModSettingController();
            string forumdesk = db.Load("Forum", "forumdesk");
            string topicdesk = db.Load("Forum", "topicdesk");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"></urlset>");

            GenSitemapTopic(doc, topicdesk);
            GenSitemapForum(doc, forumdesk);

            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + doc.OuterXml;
            return xml;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    Response.ContentType = "text/xml";
                    Response.ContentEncoding = Encoding.UTF8;
                    string xml = GenSitemap();
                    Response.Write(xml);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
